/*
 * 作者:熊猫
 * 日期:2017 - 3 - 20 10 - 58 - 0
 *
 */

package net.lainiao.morethread.Chapter8.demo4;

import java.util.concurrent.RecursiveAction;
import java.util.concurrent.TimeUnit;

/**
 * Created by Administrator on 2017/3/20.
 */
public class Task extends RecursiveAction {
    private int[] array;
    private int start, end;

    public Task(int[] array, int start, int end) {
        this.array = array;
        this.start = start;
        this.end = end;
    }

    @Override
    protected void compute() {
        if(end-start>100){
            int mid=(start+end)/2;
            Task task1=new Task(array,start,mid);
            Task task2=new Task(array,mid,end);
            task1.fork();
            task2.fork();
            task1.join();
            task2.join();
        }else{
            for (int i = start; i <end ; i++) {
                array[i]++;
                try {
                    Thread.sleep(100);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
